package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;

public class LoginPage extends Utils {
    public static String url = "http://8.137.19.140:9090/blog_login.html";
    public LoginPage() {
        super(url);
    }

    /*
    * 检查页面是否能正常访问
    * */
    public void loginPageRight() throws InterruptedException {
        //通过查看页面元素是否存在来检查页面加载成功与否
        //主页按钮
        driver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)"));
        //登录输入框
        driver.findElement(By.cssSelector("body > div.container-login > div"));
    }

    //检查登录功能：成功登录
   public void LoginSuc() throws InterruptedException {
        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#submit")).click();

        //检查登录之后是否登录成功

//       //检查“查看全文”标识
//       driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(2) > a"));
       //检查“注销“标识
       driver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)"));
       //检查“用户个人信息模块”
       driver.findElement(By.cssSelector("body > div.container > div.left > div"));
//       //通过页面标题来检查是否登录成功
//       String expect = driver.getTitle();
//       assert expect.equals("博客列表页");
//
//       driver.navigate().back();
   }

    //检查登录功能：异常登录
    public void LoginFail() throws InterruptedException {

        //通过 clear 保证输入框没有文本信息
//        driver.findElement(By.cssSelector("#username")).clear();
//        driver.findElement(By.cssSelector("#password")).clear();
        //或者直接点击“刷新”
        driver.navigate().refresh();

        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan123");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#submit")).click();
        Thread.sleep(2000);
        Alert alert = driver.switchTo().alert();
        alert.accept();
        driver.navigate().back();
//        driver.quit();
    }

}
